package com.cuitrip.apiservice;

import android.text.TextUtils;

import com.alibaba.fastjson.TypeReference;
import com.cuitrip.apiservice.model.ApiManager;
import com.cuitrip.app.rong.model.RongMessageModel;
import com.cuitrip.business.notice.model.MessageServerItem;
import com.lab.network.model.CtApiRequest;
import com.lab.network.model.CtRemoteBusiness;
import com.lab.network.model.ParseType;
import com.lab.network.proxy.ApiProxy;

import java.util.List;

import io.rong.imlib.RongIMClient;


public class MessageBusiness {

    public static void getMessageList(ApiProxy apiProxy,int userType, int start, int size){
        CtApiRequest ctApiRequest=new CtApiRequest();
        ctApiRequest.setApiName(ApiManager.MESSAGE_LIST);
        ctApiRequest.addParam("userType", userType);
        ctApiRequest.addParam("start", start);
        ctApiRequest.addParam("size", size);
        CtRemoteBusiness ctRemoteBusiness=new CtRemoteBusiness(ctApiRequest);
        //apiProxy.postAsync(ctRemoteBusiness, new ParseType(MessageServerItem.class));
        apiProxy.postAsync(ctRemoteBusiness, new ParseType(new TypeReference<List<MessageServerItem>>() {
        }.getType()));
    }

    public static void deleteMessage(ApiProxy apiProxy,String messageId){
        CtApiRequest ctApiRequest=new CtApiRequest();
        ctApiRequest.setApiName(ApiManager.MESSAGE_DEL);
        if(!TextUtils.isEmpty(messageId)){
            ctApiRequest.addParam("messageId",messageId);
        }
        CtRemoteBusiness ctRemoteBusiness=new CtRemoteBusiness(ctApiRequest);
        apiProxy.postAsync(ctRemoteBusiness, new ParseType());
    }

    public static void setMessageAsRead(ApiProxy apiProxy,String messageId, String isRead){
        CtApiRequest ctApiRequest=new CtApiRequest();
        ctApiRequest.setApiName(ApiManager.MESSAGE_READ);
        if(!TextUtils.isEmpty(messageId)){
            ctApiRequest.addParam("messageId",messageId);
        }

        ctApiRequest.addParam("isRead",isRead);
        CtRemoteBusiness ctRemoteBusiness=new CtRemoteBusiness(ctApiRequest);
        apiProxy.postAsync(ctRemoteBusiness,new ParseType());
    }

    public static void putDialog(ApiProxy apiProxy, String orderId,
                                 String receiver, String send, String content, String sid, RongMessageModel rongMessageModel, RongIMClient.ErrorCode errorCode) {
        CtApiRequest ctApiRequest = new CtApiRequest();
        ctApiRequest.setApiName(ApiManager.PUT_DIALOG);
        ctApiRequest.addParam("orderId", orderId);
        ctApiRequest.addParam("receiver", receiver);
        ctApiRequest.addParam("send", send);

        ctApiRequest.addParam("content", content);
        ctApiRequest.addParam("sid", sid);
        if (errorCode != null){
            ctApiRequest.addParam("statusMsg", "{status:" + errorCode.getValue() + "," + "client:android}");
        } else {
            ctApiRequest.addParam("statusMsg", "{status:0, client:android" + "}");
        }


        if (rongMessageModel != null) {
            ctApiRequest.addParam("isSucceed", rongMessageModel.isSuccess() ? "1" : "0");
            ctApiRequest.addParam("timeStamp", rongMessageModel.getTimeStamp());
            ctApiRequest.addParam("isFirstPost", rongMessageModel.getSendCount() == 0 ? "1" : "0");
        }
        CtRemoteBusiness ctRemoteBusiness = new CtRemoteBusiness(ctApiRequest);
        apiProxy.postAsync(ctRemoteBusiness, new ParseType());
    }

}
